<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\Request;
use bbweb\basic\BaseController;

class Upload extends BaseController
{
    public function upload(Request $req){
        $uploadType = (int)sys_config('upload_type', 1);
        $upload = new \bbweb\services\upload\Upload($uploadType, [
            'accessKey' => sys_config('accessKey'),
            'secretKey' => sys_config('secretKey'),
            'uploadUrl' => sys_config('uploadUrl'),
            'storageName' => sys_config('storage_name'),
            'storageRegion' => sys_config('storage_region'),
        ]);
        $path = $req->param('path');
        $resInfo = $upload->to($path)->validate()->move('file');
        if ($resInfo === false) {
            $this->error($upload->getError());
        }
        $res = $upload->getUploadInfo();
        $res['image_type'] = $uploadType;
        $this->success('上传成功!','',['file'=>$res['thumb_path'],'type'=>$res['type'],'size'=>$res['size']]);
    }
}